<template>
  <div class="talk">
    <button @click="getLoveTalk">获取一句土味情话</button>
    <ul>
      <li v-for="talk in talkStore.talkList" :key="talk.id">{{ talk.title }}</li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { useTalkStore } from '@/store/talk'

const talkStore = useTalkStore()

// 方法
function getLoveTalk() {
  talkStore.getTalk()
}

// 监听 pinia 数据的变化；类似于 watch 功能
talkStore.$subscribe((mutate, state) => {
  console.log('talkStore里面保存的数据发生了变化', mutate, state)
  // 写入浏览器本地存储
  localStorage.setItem('talkList', JSON.stringify(state.talkList))
})
</script>

<style>
.talk {
  background-color: rgb(164, 166, 56);
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 0 10px;
}
</style>
